/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mmogle;

import com.sun.jmx.remote.internal.ClientCommunicatorAdmin;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.awt.windows.ThemeReader;


import java.net.Socket;
import java.util.Random;



/**
 *
 * @author goia
 */
public class Main {

    static int incremento=1;

    class Cliente implements Runnable{

        Socket so;
        int clienteId;

        /* Enviando a mensagem de atualização de posição */
        public Cliente(){

            clienteId = incremento++;

          
        }



        public void run(){
            
            try{
                so = new Socket("127.0.0.1",2424);
                while(true){
                    try{
                        //System.out.println(  String.format("<cliente>Olá do cliente %d", clienteId) );
                        so.getOutputStream().write( String.format("Olá do cliente %d", clienteId).getBytes() );
                    }catch(Exception e){
                        System.out.println ("<servidor>Opss. Algum problema aconteceu: " + e.getMessage());
                    }

                   Thread.sleep( new Random().nextInt(1000) );

                    
                }
            }catch(Exception e){}
            
        }
    }


    /*
import java.net.Socket;
import java.net.ServerSocket;

public class server{
	public static void main(String args[]) throws Exception{
		ServerSocket ss = new ServerSocket(2424);
		while(true){
			byte b[] = new byte[1024];
			Socket so = ss.accept();
			so.getInputStream().read(b);
			System.out.println(new String(b));
		}
	}
}*/
    
    class Servidor implements Runnable{

        public Servidor(){
               Thread cur=Thread.currentThread();
               cur.setPriority(Thread.MAX_PRIORITY);
        }

        public void run(){

            try{
                ServerSocket ss = new ServerSocket(2424);
		while(true){
			byte b[] = new byte[1024];
			Socket so = ss.accept();
			so.getInputStream().read(b);

                        System.out.println("Recebeu do cliente: ");
			System.out.println(new String(b));
		}
            }catch(Exception ex){
            
            }
        }

    }

    public Main(){
         Servidor servidor = new Servidor();
        

         

         ExecutorService threadPull         = Executors.newFixedThreadPool( 101 );
         threadPull.execute(servidor);
         
         //threadPull.execute(new Cliente());
         for( int i=0; i<5; i++){
             threadPull.execute(new Cliente());
         }

         
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Main m = new Main();
        

    }

}
